/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.shift.rn;

import bpmlab.shift.entity.Lote;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 *
 * @author hugolima
 */
public class RelatorioRN {

    public static JasperPrint relatorioCardapio() throws JRException, SQLException {

        Connection conexao = getConnection();

        Map<String, Object> parametros = new HashMap<String, Object>();
//        par.put("algum parametro do relatorio", objeto);

        URL relatorio = RelatorioRN.class.getResource("resources/cardapio.jasper");
        JasperReport jr = (JasperReport) JRLoader.loadObject(relatorio);
        JasperPrint jp = JasperFillManager.fillReport(jr, parametros, conexao);

        return jp;
    }

    private static Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/shift", "root", "root");
    }

    public static void relatorioPesagem(List<Lote> lotes, String titulo) throws JRException, SQLException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        JRDataSource ds;
        String relatorio;
        HashMap parametros = new HashMap();
        if (lotes == null || lotes.isEmpty()) {
            ds = new JREmptyDataSource();
            relatorio = facesContext.getExternalContext().getRealPath("/WEB-INF/classes/relatorios/relatorioSemDados.jasper");
        } else {
            ds = new JRBeanCollectionDataSource(lotes);
            relatorio = facesContext.getExternalContext().getRealPath("/WEB-INF/classes/relatorios/pesagem.jasper");
            parametros.put("titulo", titulo);
        }
        JasperPrint jasperPrint = JasperFillManager.fillReport(relatorio, parametros, ds);
        gerarPdf(JasperExportManager.exportReportToPdf(jasperPrint));
    }

    public static void relatorioSecagem(List<Lote> lotes, String titulo) throws JRException, SQLException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        JRDataSource ds;
        String relatorio;
        HashMap parametros = new HashMap();
        if (lotes == null || lotes.isEmpty()) {
            ds = new JREmptyDataSource();
            relatorio = facesContext.getExternalContext().getRealPath("/WEB-INF/classes/relatorios/relatorioSemDados.jasper");
        } else {
            ds = new JRBeanCollectionDataSource(lotes);
            relatorio = facesContext.getExternalContext().getRealPath("/WEB-INF/classes/relatorios/secagem.jasper");
            parametros.put("titulo", titulo);
        }
        JasperPrint jasperPrint = JasperFillManager.fillReport(relatorio, parametros, ds);
        gerarPdf(JasperExportManager.exportReportToPdf(jasperPrint));
    }

    private static void gerarPdf(byte[] bytes) {
        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpServletResponse res = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            res.setContentType("application/pdf");
            res.setHeader("Content-disposition", "inline;filename=arquivo.pdf");
            res.setContentLength(bytes.length);
            res.setCharacterEncoding("utf-8");
            res.getOutputStream().write(bytes);
            facesContext.responseComplete();
        } catch (IOException ex) {
            Logger.getLogger(RelatorioRN.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
